package com.wink_172.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.View.OnFocusChangeListener
import androidx.databinding.DataBindingUtil
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivityChatBinding
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.model.Constants

/**
 *
 */
class ChatCommonActivity : SmartActivity() {
    lateinit var binding: ActivityChatBinding
    companion object {    private const val TAG = "ChatCommonActivity"
        open fun startActivity(context: Context) {

            //apply用法 kotlin-P132
            val intent = Intent(context, ChatCommonActivity::class.java).apply {
//                putExtra(Constants.PARAM_DATA1, "hello01");
//                putExtra(Constants.PARAM_DATA2, "hello02");
//                putExtra(Constants.PARAM_DATA3, "hello03");
            }

            context.startActivity(intent)
        }

    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_chat)

        initView()
    }
    private fun initView() {

        binding!!.btnSend.setOnClickListener {
            //         发送文字
        }
        binding!!.btnVoice.setOnClickListener { //隐藏emjio面板
            //隐藏键盘面板
            //隐藏更多面板
            hideEmjioPanel()
            hideMoreFunPanel()
            hideSoftPanel()
            binding!!.btnHoldTalk.visibility = View.VISIBLE
            binding!!.btnEmjio.setVisibility(View.VISIBLE)
            binding!!.btnKeyboard2.setVisibility(View.GONE)
            binding!!.btnKeyboard1.setVisibility(View.VISIBLE)
            binding!!.btnVoice.setVisibility(View.GONE)
        }

        binding!!.btnKeyboard1.setOnClickListener { //隐藏emjio面板
            //显示键盘面板
            //隐藏更多面板
            hideEmjioPanel()
            hideMoreFunPanel()

            showSoftPanel()
            binding.input1.visibility = View.VISIBLE
            binding!!.btnHoldTalk.visibility = View.GONE
            binding!!.btnEmjio.setVisibility(View.VISIBLE)
            binding!!.btnKeyboard2.setVisibility(View.GONE)
            binding!!.btnKeyboard1.setVisibility(View.GONE)
            binding!!.btnVoice.setVisibility(View.VISIBLE)
        }
        binding!!.btnKeyboard2.setOnClickListener { //隐藏emjio面板
            //展示键盘面板
            //隐藏更多面板
            hideEmjioPanel()
            hideMoreFunPanel()
            showSoftPanel()
            binding.input1.visibility = View.VISIBLE

            binding!!.btnKeyboard2.setVisibility(View.GONE)
            binding!!.btnEmjio.setVisibility(View.VISIBLE)
        }
        binding!!.btnMore.setOnClickListener { //隐藏emjio面板
            //隐藏键盘面板
            //展示更多面板
            hideEmjioPanel()
            showMoreFunPanel()
            hideSoftPanel()
            binding!!.btnHoldTalk.visibility = View.GONE
            binding!!.btnEmjio.setVisibility(View.VISIBLE)
            binding!!.btnKeyboard2.setVisibility(View.GONE)
            binding!!.btnKeyboard1.setVisibility(View.GONE)
            binding!!.btnVoice.setVisibility(View.VISIBLE)
        }


        binding!!.btnEmjio.setOnClickListener { //展示emjio面板
            //隐藏键盘面板
            //隐藏更多面板
            showEmjioPanel()
            hideMoreFunPanel()
            hideSoftPanel()
            binding.input1.visibility=View.VISIBLE
            binding!!.btnHoldTalk.visibility = View.GONE
            binding!!.btnEmjio.setVisibility(View.GONE)
            binding!!.btnVoice.setVisibility(View.VISIBLE)
            binding!!.btnKeyboard1.setVisibility(View.GONE)
            binding!!.btnKeyboard2.setVisibility(View.VISIBLE)
        }

        binding!!.input1.setOnFocusChangeListener(OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                showSoftPanel()
                hideEmjioPanel()
                hideMoreFunPanel()
            } else {

            }
        })
        // 监听输入框的内容变化
        binding!!.input1.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                // 输入框内容变化时，根据内容长度显示或隐藏发送按钮和表情按钮
                if (s.length > 0) {
                    binding!!.btnMore.setVisibility(View.GONE)
                    binding!!.btnSend.visibility = View.VISIBLE
                } else {
                    binding!!.btnMore.setVisibility(View.VISIBLE)
                    binding!!.btnSend.visibility = View.GONE
                }
            }

            override fun afterTextChanged(s: Editable) {}
        })
    }

    fun showSoftPanel() {
        binding.input1.requestFocus()

        binding.input1.showSoftInput()
        binding.softContainer.visibility = View.VISIBLE
    }

    fun hideSoftPanel() {
        binding.input1.hideSoftInput()
        binding.softContainer.visibility = View.GONE
    }

    fun showEmjioPanel() {
        binding.emjioContainer.visibility = View.VISIBLE
    }

    fun hideEmjioPanel() {
        binding.emjioContainer.visibility = View.GONE
    }

    fun showMoreFunPanel() {
        binding.moreFunContainer.visibility = View.VISIBLE
    }

    fun hideMoreFunPanel() {
        binding.moreFunContainer.visibility = View.GONE
    }


}